<?php
/**
 * 后台用户操作日志模型类
 *
 * @author liang<lang@vip.deyi.com>
 * @since   2016/12/1 10:40
 * @copyright  deyi.com
 */


namespace Apps\Acp\Models;


use Apps\Common\Model\BaseModel;

/**
 * Class ManagerLog
 * @package Apps\Acp\Models
 */
class ManagerLog extends BaseModel
{
    // 定义此model访问的数据库名
    protected static $_DBNAME = 'yyx_fanli';

    // 定义此model访问的表名
    protected static $_TABLENAME = 'acp_manager_log';

    public function record($uid, $ip, $log)
    {
        return $this->insertByFields([
            'uid' => $uid,
            'ip' => ip2long($ip),
            'log' => $log,
            'dateline' => time()
        ]);
    }

    public function getListByConds($conditions, $offset, $limit)
    {
        list($values, $conds, $likeConds) = $this->assembleConditionsData($conditions);
        $where = $this->assembleComplexWhereSql($conds, $likeConds);

        $sql = 'SELECT uid,INET_NTOA(ip) AS ip,log,dateline FROM ' . self::$_TABLENAME . $where . ' ORDER BY id DESC ' . $this->assembleLimit($offset, $limit);

        return $this->select($sql, $values);
    }

    public function countListByConds($conditions)
    {
        list($values, $conds, $likeConds) = $this->assembleConditionsData($conditions);
        $where = $this->assembleComplexWhereSql($conds, $likeConds);

        $sql = 'SELECT COUNT(*) AS num FROM ' . self::$_TABLENAME . $where;

        return $this->selectCount($sql, $values);
    }

    public function deleteByDateline($dateline)
    {
        $sql = 'DELETE FROM ' . self::$_TABLENAME . ' WHERE dateline<=?';

        return $this->execute($sql, [$dateline]);
    }
}